﻿<h1>Introduction to Source Code Elements</h1>

<p>
Go is known for its simple and clean syntax.
This article introduces the common source code elements in
programming through a simple example. This will help new gophers
(Go programmers) get a basic idea of the usage of Go elements.
</p>

<h3>Programming and Source Code Elements</h3>

<p>
Programming can be viewed as manipulating
operations in all kinds of ways to reach certain goals.
Operations write data to and read data from
hardware devices to complete tasks.
For modern computers, elemental operations are low-level
CPU and GPU instructions.
Common hardware devices include memory, disk, network card, graphics card,
monitor, keyboard and mouse, etc.
</p>

<p>
Programming by manipulating low-level instructions directly
is tedious and error-prone.
High-level programming languages make
some encapsulations for low-level operations,
and make some abstracts for data,
to make programming more intuitive and human-friendly.
</p>

<p>
In popular high-level programming languages,
operations are mainly achieved by calling
<b>functions</b> and using <b>operators</b>.
Most popular high-level programming languages support several kinds of
conditional and loop <b>control flows</b>,
we can think of them as special operations.
The syntax of these control flows is close to human language
so that the code written by programmers is easy to understand.
</p>

<p>
Data is abstracted as <b>types</b> and <b>values</b>
in most high-level programming languages.
Types can be viewed as value templates,
and values can be viewed as type instances.
Most languages support several built-in types, and also support custom types.
The type system of a programming language is the spirit of the language.
</p>

<p>
There may be a large number of values used in programming.
Some of them can be represented with their <b>literals</b>
(text representations) directly, but others can't.
To make programming flexible and less error-prone, many values are named.
Such values include <b>variables</b> and named <b>constants</b>.
</p>

<p>
Named functions, named values (including variables and named constants),
defined types and type alias are called <b>resources</b> in Go 101.
The names of resources must be
<b><a href="keywords-and-identifiers.html#identifier">identifiers</a></b>.
Package names and package import names shall also be identifiers.
</p>

<p>
High-level programming code will be translated to low-level CPU instructions
by compilers to get executed.
To help compilers parse high-level programming code,
many words are reserved to prevent them from being used as identifiers.
Such words are called
<b><a href="keywords-and-identifiers.html#keyword">keywords</a></b>.
</p>

<p>
Many modern high-level programming languages
use <b>packages</b> to organize code.
A package must <b>import</b> another package
to use the exported (public) resources in the other package.
Package names and package import names shall also be identifiers.
</p>

<p>
Although the code written in high-level programming languages is
more understandable than low-level machine languages,
we still need some comments for some code to explain the logic.
The example program in the next section contains many comments.
</p>

<h3>A Simple Go Demo Program</h3>

<p>
Let's view a short Go demo program to know all kinds of code elements in Go.
Like some other languages, in Go, line comments start with <code>//</code>,
and each block comment is enclosed in a pair of <code>/*</code> and <code>*/</code>.
</p>

<div>

Below is the demo Go program.
Please read the comments for explanations.
More explanations are following the program.

<pre class="line-numbers must-line-numbers"><code class="language-go">package main // specify the source file's package

import "math/rand" // import a standard package

const MaxRnd = 16 // a named constant declaration

// A function declaration
/*
 StatRandomNumbers produces a certain number of
 non-negative random integers which are less than
 MaxRnd, then counts and returns the numbers of
 small and large ones among the produced randoms.
 n specifies how many randoms to be produced.
*/
func StatRandomNumbers(n int) (int, int) {
	// Declare two variables (both as 0).
	var a, b int
	// A for-loop control flow.
	for i := 0; i < n; i++ {
		// An if-else control flow.
		if rand.Intn(MaxRnd) < MaxRnd/2 {
			a = a + 1
		} else {
			b++ // same as: b = b + 1
		}
	}
	return a, b // this function return two results
}

// "main" function is the entry function of a program.
func main() {
	var num = 100
	// Call the declared StatRandomNumbers function.
	x, y := StatRandomNumbers(num)
	// Call two built-in functions (print and println).
	print("Result: ", x, " + ", y, " = ", num, "? ")
	println(x+y == num)
}
</code></pre>

Save above source code to a file named
<code>basic-code-element-demo.go</code> and run this program by:
<pre class="output"><code>$ go run basic-code-element-demo.go
Result: 46 + 54 = 100? true
</code></pre>

<p></p>

<p>
In the above program, <code>package</code>, <code>import</code>, <code>const</code>,
<code>func</code>, <code>var</code>, <code>for</code>, <code>if</code>,
<code>else</code>, and <code>return</code> are all keywords.
Most other words in the program are identifiers.
Please read <a href="keywords-and-identifiers.html">keywords and identifiers</a>
for more information about keywords and identifiers.
</p>

<!-- NOTE to translator: the line numbers may be changed for a localized language -->

<p>
The four <code>int</code> words at line <i>15</i> and line <i>17</i>
denote the built-in <code>int</code> type, one of many kinds of integer types in Go.
The <code>16</code> at line <i>5</i>, <code>0</code> at line <i>19</i>,
<code>1</code> at line <i>22</i> and <code>100</code> at line <i>32</i> are some integer literals.
The <code>"Result: "</code> at line <i>36</i> is a string literal.
Please read <a href="basic-types-and-value-literals.html">basic types and their value literals</a>
for more information about above built-in basic types and their value literals.
Some other types (composite types) will be introduced later in other articles.
</p>

<p>
Line <i>22</i> is an assignment.
Line <i>5</i> declares a named constant, <code>MaxRnd</code>.
Line <i>17</i> and line <i>32</i> declare three variables,
with the standard variable declaration form.
Variables <code>i</code> at line <i>19</i>, <code>x</code> and <code>y</code> at line <i>34</i>
are declared with the short variable declaration form. We have specified the type
for variables <code>a</code> and <code>b</code> as <code>int</code>.
Go compiler will deduce that the types of <code>i</code>, <code>num</code>,
<code>x</code> and <code>y</code> are all <code>int</code>,
because they are initialized with integer literals.
Please read <a href="constants-and-variables.html">constants and variables</a>
for more information about untyped values, type deduction,
value assignments, and how to declare variables and named constants.
</p>

<p>
There are many operators used in the program, such as the less-than
comparison operator <code>&lt;</code> at line <i>19</i> and <i>21</i>,
the equal-to operator <code>==</code> at line <i>37</i>,
and the addition operator <code>+</code> at line <i>22</i> and line <i>37</i>.
Yes, <code>+</code> at line <i>36</i> is not an operator,
it is one character in a string literal.
The values involved in an operator operation are called operands.
Please read <a href="operators.html">common operators</a> for more information.
More operators will be introduced in other articles later.
</p>

<p>
At line <i>36</i> and line <i>37</i>, two built-in functions,
<code>print</code> and <code>println</code>, are called.
A custom function <code>StatRandomNumbers</code> is declared from line
<i>15</i> to line <i>28</i>, and is called at line <i>34</i>.
Line <i>21</i> also calls a function, <code>Intn</code>,
which is a function declared in the <code>math/rand</code> standard package.
A function call is a function operation.
The input values used in a function call are called arguments.
Please read <a href="function-declarations-and-calls.html">function
declarations and calls</a> for more information.
</p>

<p><i>
(Note, the built-in <code>print</code> and <code>println</code> functions
are not recommended to be used in formal Go programming.
The corresponding functions in the <code>fmt</code> standard packages
should be used instead in formal Go projects.
In Go 101, the two functions are only used in the several starting articles.)
</i></p>

<p>
Line <i>1</i> specifies the package name of the current source file.
The <code>main</code> entry function must be declared in a package
which is also called <code>main</code>.
Line <i>3</i> imports a package, the <code>math/rand</code> standard code package.
Its import name is <code>rand</code>.
The function <code>Intn</code> declared in this
standard package is called at line <i>21</i>.
Please read <a href="packages-and-imports.html">code packages and package imports</a>
for more information about how to organize code packages and import packages.
</p>

<p>
The article <a href="expressions-and-statements.html">expressions, statements
and simple statements</a> will introduce what are expressions and statements.
In particular, all kinds of simple statements, which are special statements,
are listed.
Some portions of all kinds of control flows must be simple statements,
and some portions must be expressions.
</p>

<p>
In the <code>StatRandomNumbers</code> function body, two control flows are used.
One is a <code>for</code> loop control flow, which nests
the other one, an <code>if-else</code> conditional control flow.
Please read <a href="control-flows.html">basic control flows</a>
for more information about all kinds of basic control flows.
Some other special control flows will be introduced in other articles later.
</p>

<p>
Blank lines have been used in the above program to improve the readability of the code.
And as this program is for code elements introduction purpose,
there are many comments in it.
Except the documentation comment for the <code>StatRandomNumbers</code> function,
other comments are for demonstration purpose only.
We should try to make code self-explanatory
and only use necessary comments in formal projects.
</p>
</div>

<a class="anchor" id="line-break"></a>
<h3>About Line Breaks</h3>

<div>
Like many other languages, Go also uses a pair of braces (<code>{</code> and <code>}</code>)
to form an explicit code block. However, in Go programming, coding style can't be
arbitrary. For example, many of the starting curly braces (<code>{</code>)
can't be put on the next line. If we modify the <code>StatRandomNumbers</code>
function declaration in the above program as the following,
the program will fail to compile.

<pre class="line-numbers"><code class="language-go">func StatRandomNumbers(n int) (int, int)
{ // syntax error
	var a, b int
	for i := 0; i < n; i++
	{ // syntax error
		if rand.Intn(MaxRnd) < MaxRnd/2
		{ // syntax error
			a = a + 1
		} else {
			b++
		}
	}
	return a, b
}
</code></pre>

<p></p>
Some programmers may not like the line break restrictions.
But the restrictions have two benefits:
<ol>
<li>
	they make code compilations become faster.
</li>
<li>
	they make the coding styles written by different gophers look similar,
	so that it is more easily for gophers to read and understand
	the code written by other gophers.
</li>
</ol>

<p>
We can learn more about line break rules in
<a href="line-break-rules.html">a later article</a>.
At present, we should avoid putting a starting curly brace on a new line.
In other words, generally, the first non-blank character of a code line
should not be the starting curly brace character.
(But, please remember, this is not a universal rule.)
</p>

</div>

